package com.codinghero.acm.etc;

import java.util.Scanner;

class BalanceExam {
	private static final int BALANCE_WEIGH = 10;
	
	private int n;
	private int[][][] input;
	private int[] balance;
	private int[][] output;
	
	public void execute() {
		loadData();
		for (int i = 0; i < n; i++) {
			if (balance[i] == 0)
				cal(i);
		}
		for (int i = 0; i < n; i++) {
			System.out.println(output[i][0] + " " + output[i][1]);
		}
	}
	
	private void loadData() {
		Scanner cin = new Scanner(System.in);
		String line = cin.nextLine();
		n = Integer.parseInt(line);
		input = new int[n][2][n + 1];
		balance = new int[n];
		output = new int[n][2];
		for (int i = 0; i < n * 2; i++) {
			line = cin.nextLine();
			String[] data = line.split(" ");
			for (int j = 0; j <= n; j++) {
				if (j < data.length)
					input[(i - i % 2) / 2][i % 2][j] = Integer.parseInt(data[j]);
				else
					input[(i - i % 2) / 2][i % 2][j] = -1;
			}
		}
	}
	
	private void cal(int i) {
		int leftSide = input[i][0][0];
		// cal leftSide
		for (int j = 1; j <= n; j++) {
			if (input[i][0][j] == -1)
				break;
			else {
				if (balance[input[i][0][j]] == 0)
					cal(input[i][0][j]);
				leftSide += balance[input[i][0][j]];
			}
		}

		// cal rightSide
		int rightSide = input[i][1][0];
		for (int j = 1; j <= n; j++) {
			if (input[i][1][j] == -1)
				break;
			else {
				if (balance[input[i][1][j]] == 0)
					cal(input[i][1][j]);
				rightSide += balance[input[i][1][j]];
			}
		}
		
		// cal output & balance
		if (leftSide >= rightSide) {
			output[i][1] = leftSide - rightSide;
			balance[i] = leftSide * 2 + BALANCE_WEIGH;
		} else if (leftSide < rightSide) {
			output[i][0] = rightSide - leftSide;
			balance[i] = rightSide * 2 + BALANCE_WEIGH;
		}
	}
	
	public static void main(String[] args) {
		new BalanceExam().execute();
	}
}
